<template>
  <!-- 新增编辑 -->
  <a-modal
    width="95%"
    height="90%"
    :maskClosable="true"
    :visible="props.visible"
    :confirm-loading="loading"
    :forceRender="true"
    :title="props.data.diseaseName+'             患者：'+props.data.name"
    :body-style="{ paddingBottom: '8px', height: '520px', overflowY: 'auto' }"
    @update:visible="updateVisible"
  >
    <template #footer>
      <a-button @click="updateVisible(false)">取消</a-button>
      <a-button type="primary" @click="update">草稿</a-button>
      <a-button type="primary" @click="save">提交</a-button>
    </template>

    <position-form v-model:form="form" ref="positionFormRef"></position-form>
  </a-modal>
</template>

<script setup name="PatientAddEdit">
import { ref, onMounted } from 'vue';
import PositionForm from './patient-form.vue';
import { message } from 'ant-design-vue';
import { PatientApi } from '../api/PatientApi';
import { getBusinessMaxSort } from '@/utils/common/util';

const props = defineProps({
  formList: [],
  visible: Boolean,
  data: Object
});

const emits = defineEmits(['update:visible', 'done']);
// 弹框加载
const loading = ref(false);
// 是否是编辑状态
const isUpdate = ref(false);
// 表单数据
const form = ref({});
const patient = ref({});
// ref
const positionFormRef = ref(null);
const diseaseList = ref([]);
onMounted(async() => {

  if (props.data) {
    isUpdate.value = true;

    patient.value = Object.assign({}, props.data);
    form.value = Object.assign({}, props.data);

  } else {
    form.value.positionSort = await getBusinessMaxSort('SYSTEM_HR_POSITION');
    isUpdate.value = false;
  }
});

// 更改弹框状态
const updateVisible = value => {
  emits('update:visible', value);
};
const update = async () => {
  let result = null;
  result = PatientApi.update(form.value);
  result
      .then(async result => {
        // 移除加载框
        loading.value = false;
        // 提示添加成功
        message.success(result.message);

      })
      .catch(() => {
        loading.value = false;
      });
}
const isDate1BeforeDate2 = (date1,date2) => {
  console.log(date1)
  console.log(Date(date1))
  console.log(date2)
  console.log(Date(date2))
  return new Date(date1) < new Date(date2);
}
// 点击保存
const save = async () => {
  console.log('--------------form---------------------')
  console.log(JSON.stringify(form))
  console.log('--------------form.data---------------------')
  console.log(JSON.stringify(form.value.data))
  positionFormRef.value.$refs.formRef.validate().then(async valid => {
    if (valid) {
      // 修改加载框为正在加载
      loading.value = true;

      let result = null;
      let msg ='';
      if(!isDate1BeforeDate2(form.value.data['CM-0-2-4-1'],form.value.data['CM-0-2-4-2'])){
        console.log(isDate1BeforeDate2(form.value.data['CM-0-2-4-1'],form.value.data['CM-0-2-4-2']))
        console.log(isDate1BeforeDate2(form.value.data['CM-0-2-4-2'],form.value.data['CM-0-2-4-1']))
        msg = '出院日期应大于入院日期'
      }
      if(form.value.data['CM-0-2-6-1']!=null){
        if(!isDate1BeforeDate2(form.value.data['CM-0-2-6-1'],form.value.data['CM-0-2-6-2'])){
          msg = '手术结束日期时间应大于手术开始日期时间'
        }
        if(!isDate1BeforeDate2(form.value.data['CM-0-2-4-1'],form.value.data['CM-0-2-6-1'])){
          msg = '手术开始日期时间应大于入院日期时间'
        }
      }


      if(form.value.diseaseCode == 'CS') {

        if (form.value.data['CS-1-2-6'] != null && form.value.data['CS-1-2-6'] != '' && form.value.data['CS-1-2-6'] != 'N') {
          msg = '该病例不符合上报要求，原因：孕妇伴有特殊感染ICD-10编码及诊断名称不符合剖宫产上报条件，请前往HBV 感染分娩母婴阻断进行上报'
        }

        if (!isDate1BeforeDate2(form.value.data['CM-0-2-6-1'], form.value.data['CS-2-4-2'])) {
          msg = '胎儿娩出日期时间应大于等于手术开始日期时间'
        }
        if (form.value.data['CM-0-2-3-2'] != null && form.value.data['CM-0-2-3-2'] != '') {
          if (isDate1BeforeDate2(form.value.data['CM-0-2-3-2'], form.value.data['CS-0-2-2'])) {
            msg = '到达本院急诊或者门诊日期时间应大于末次月经日期'
          }
          if (isDate1BeforeDate2(form.value.data['CM-0-2-4-1'], form.value.data['CM-0-2-3-2'])) {
            msg = '入院日期时间应大于到达本院急诊或者门诊日期时间'
          }
        }
        if (isDate1BeforeDate2(form.value.data['CM-0-2-6-2'], form.value.data['CS-2-4-2'])) {
          msg = '胎儿娩出日期时间应小于等于手术结束（缝皮结束）日期时间'
        }
      }
      if(form.value.data['CM-0-1-5']=='y'){
        msg = '该病例不符合上报要求，原因：该病例判定为重复住院，无需上报'
      }
      if(form.value.diseaseCode == 'ICH'){
        if(form.value.data['ICH-1-2-5']!=null){
          if(isDate1BeforeDate2(form.value.data['ICH-1-2-5'],form.value.data['ICH-1-1-1'])){
            msg = '评估日期时间应大于等于本院急诊医师接诊日期时间'
          }
        }
      }
      if(form.value.diseaseCode == 'Cap-Adult'){
        if(form.value.data['Cap-Adult-1-3-1']=='n'){
          message.error('该病例不符合上报要求')
        }
      }

      if(msg == ''){
        // 执行编辑或修改
        result = PatientApi.edit(form.value);

        result
            .then(async result => {
              // 移除加载框
              loading.value = false;

              // 提示添加成功
              message.success(result.message);
              // 关闭弹框，通过控制visible的值，传递给父组件
              updateVisible(false);

              // 触发父组件done事件
              emits('done');
            })
            .catch(() => {
              loading.value = false;
            });
      }else{
        message.error(msg);
      }

    }
  });
};
</script>

<style></style>
